<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Section extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('muser');
   $this->load->library('form_validation');
   $this->load->library('pagination');
 }
 function index(){ 	
 	 if ($this->input->post('Session_ID')!=NULL)
 	 {
 	 	$this->form_validation->set_rules('Session_ID','Session_ID','trim|callback_check_Session');
	if ($this->form_validation->run()==FALSE){
			$data['error']='Section is existed!';
		}else{
			$DT['Session_ID']=$this->input->post('Session_ID');	
		 	$DT['SessionName']=$this->input->post('SessionName');			
			$DT['Department_ID']=$this->input->post('Department_ID');
			$this->muser->addData('Session',$DT);
			$data['error']='insert successful!';
		} 	
			
 	 }	
 	 $config['base_url'] = base_url().'section/index'; 
	 $config['total_rows'] = $this->muser->num_rows('Session');
	 
	 $config['per_page'] = 5; // xác định số record ở mỗi trang  
     $config['uri_segment'] = 3; // xác định segment chứa page number  
	 $this->pagination->initialize($config); 
	 $data['result']=$this->muser->getalldata('Session',$config['per_page'],$this->uri->segment(3));	
	
	 $data['Department']=$this->getDepartmentID();
	  if ($error=$this->session->userdata('error')) {
	 	$data['error']=$error;
		$this->session->unset_userdata('error'); 
	 }
	 $this->load->view('_header');
     $this->load->view('section/section_view',$data); 
     $this->load->view('_footer');
 }
 function delete($Session_ID){
	if ($this->muser->checkData('Contract','Session_ID',$Session_ID)){
		$this->muser->delColumn('Session','Session_ID',$Session_ID);
		redirect('section/index');
	}else{
		$error['error']= $Session_ID.' is a foreign key!';
		$this->session->set_userdata($error);
		redirect('section/index');
	}	
 }
 
function edit1 ()
 {
 	$DT['Session_ID']=$this->input->post('Session_ID');	
	$DT['SessionName']=$this->input->post('SessionName');			
	$DT['Department_ID']=$this->input->post('Department_ID');
	$this->muser->updateData('Session','Session_ID',$DT['Session_ID'],$DT);	
	redirect('section/index');		
 } 
 function edit($str)
 {	
 	$result=$this->muser->getRdata('Session','Session_ID',$str);
	$data['result']=$result;
	$data['department']=$this->getDepartmentID();	
 	$this->load->view('_header');
	$this->load->view('section/editSection_view',$data);	
	$this->load->view('_footer',$data);
 }
 
 function check_Session($str){
 		return $this->muser->checkData('Session','Session_ID',$str);		
 }
 function getDepartmentID(){
 	 $session1=$this->muser->getData('Department');
	 foreach ($session1 as $row) {
		 $Department[$row->Department_ID]=$row->Department_ID.'-'.$row->Department;
	 }
	 return $Department;
 }
}